﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crossfader
{
    public class ContentControl : Control
    {
        public double ResizeTranformFactor = 1.3;
        public ScaleTransform SizeTransform = null;
        public DoubleAnimation ScaleXAnimation = null;
        public DoubleAnimation ScaleYAnimation = null;
        public DoubleAnimation ContentPlaneTransformAnimation = null;
        public DoubleAnimation MyContentOpacity = null;
        public Storyboard SizeAnimation = null;
        public Grid MyContentPresenter = null;
        private bool CanRoll = true;

        public bool Ready = false;

        public ContentControl()
        {
            DefaultStyleKey = typeof(ContentControl);
        }

        public override void OnApplyTemplate()
        {
            SizeTransform = ((ScaleTransform)GetTemplateChild("SizeTransform"));
            ScaleXAnimation = ((DoubleAnimation)GetTemplateChild("ScaleXAnimation"));
            ScaleYAnimation = ((DoubleAnimation)GetTemplateChild("ScaleYAnimation"));
            ContentPlaneTransformAnimation = ((DoubleAnimation)GetTemplateChild("ContentPlaneTransformAnimation"));
            MyContentOpacity = ((DoubleAnimation)GetTemplateChild("MyContentOpacity"));
            SizeAnimation = ((Storyboard)GetTemplateChild("SizeAnimation"));
            MyContentPresenter = ((Grid)GetTemplateChild("MyContentPresenter"));

            MyContentPresenter.MouseLeftButtonUp += new MouseButtonEventHandler(MyContentPresenter_MouseLeftButtonUp);

            Ready = true;
            base.OnApplyTemplate();
        }

        void MyContentPresenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (CanRoll)
            {
                double ThisWidth = ((Grid)((AnimatingCloudPanel)(this.Parent)).Parent).ActualWidth;
                Point MyPoint = e.GetPosition(((Grid)((AnimatingCloudPanel)(this.Parent)).Parent));

                if (MyPoint.X > (ThisWidth / 2.0))
                {
                    ((AnimatingCloudPanel)(this.Parent)).RoleBack();
                }
                else
                {
                    ((AnimatingCloudPanel)(this.Parent)).RoleForward();
                }
            }
        }

        public void SetSizeTransform(double RelativeSizeMe)
        {
            if (Ready)
            {
                //ScaleXAnimation.From = ScaleXAnimation.To;
                //ScaleXAnimation.To = RelativeSizeMe;
                //ScaleYAnimation.From = ScaleYAnimation.To;
                //ScaleYAnimation.To = RelativeSizeMe;

                if (RelativeSizeMe == 1.0)
                {
                    //ContentPlaneTransformAnimation.To = 0.0;
                    //MyContentOpacity.To = 1.0;
                    this.Visibility = Visibility.Visible;
                    this.Opacity = 1.0;
                    CanRoll = false;
                }
                else if (RelativeSizeMe == 0.0)
                {
                    //ContentPlaneTransformAnimation.To = 90;
                    //MyContentOpacity.To = 0.0;
                    this.Visibility = Visibility.Collapsed;
                    //this.Opacity = 0.5;
                    CanRoll = false;
                }
                else
                {
                    //ContentPlaneTransformAnimation.To = 90;
                    // MyContentOpacity.To = 0.5;
                    this.Visibility = Visibility.Visible;
                    this.Opacity = 0.5;
                    CanRoll = true;
                }

                //SizeAnimation.Begin();

                if (RelativeSizeMe != 1)
                {

                }
            }
        }

        public static readonly DependencyProperty DisplayContentProperty = DependencyProperty.Register("DisplayContent", typeof(UIElement), typeof(ContentControl), new PropertyMetadata(null));

        public UIElement DisplayContent
        {
            get { return (UIElement)GetValue(DisplayContentProperty); }
            set { SetValue(DisplayContentProperty, value); }
        } 
    }
}
